<?php

/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2023-02-22
 * Time: 16:14:34
 * Info:
 */

namespace app\index\controller;

use app\common\model\Models;
use app\common\model\Content as ContentModel;
use think\facade\Db;
use think\facade\View;
use libs\Library;
use app\common\model\Category as CategoryModel;
use app\common\model\CategoryChildid as CategoryChildidModel;

class Index extends BaseController
{

    protected $seo;

    public function initialize()
    {
        parent::initialize();
        // 非开发者模式下,屏蔽非致命错误
        if ( ! $this->app->isDebug()) {
            // 屏蔽掉notice错误
            //error_reporting(E_ERROR & ~E_WARNING & ~E_NOTICE);
            error_reporting(E_ERROR);
        }

        $this->seo = [
            'title'       => get_config('site_name'),
            'keywords'    => get_config('site_keyword'),
            'description' => get_config('site_description')
        ];
        View::assign([
            'cid' => 0,
            'seo' => $this->seo
        ]);
    }

    public function index()
    {
        return $this->view->fetch('/index');
    }

    /**
     * 列表页
     *
     * @param $cid
     */
    public function lists()
    {
        // 栏目英文目录
        $catname = $this->request->param('catname', '');
        // 栏目ID
        $cid  = $this->request->param('catid', '');
        $data = CategoryModel::with([
            'childids' => function ($query) {
                $query->field('*');
            }
        ])->where(function ($query) use ($catname, $cid) {
            if ( ! empty($cid)) {
                $query->where('id', $cid);
            } else {
                $query->where('dirname', $catname);
            }
        })->find();
        if (empty($data)) {
            $this->error('获取数据失败');
        }

        //获取seo信息
        $this->seo['title']       = ! empty($data['seo_title']) ? $data['seo_title'] : $data['name']."-".$this->seo['title'];
        $this->seo['keywords']    = ! empty($data['seo_keywords']) ? $data['seo_keywords'] : $this->seo['keywords'];
        $this->seo['description'] = ! empty($data['seo_desc']) ? $data['seo_desc'] : $this->seo['description'];

        //获取模版信息（如果有子级栏目，取栏目模版（频道模版），否则取列表模版）
        if ( ! empty($data['childids'])) {
            $listTpl = "/".$data['index_tpl'];
        } else {
            $listTpl = "/".$data['list_tpl'];
        }

        View::assign('cid', $data['id']);
        View::assign('pid', $data['pid']);
        View::assign('model_id', $data['model_id']);
        View::assign('seo', $this->seo);
        View::assign('cate', $data);

        if (isset($data['model_id']) && $data['model_id'] === 2) {
            $content = ContentModel::where('cate_id', $data['id'])->find();
            View::assign('content', $content);
        }

        return $this->view->fetch($listTpl);
    }

    /**
     * 详情页
     */
    public function show()
    {
        // 栏目ID
        $catId = $this->request->param('catid', '', 'intval');
        // 栏目英文
        $list = $this->request->param('catname', '');
        // 父级栏目
        $catdir = $this->request->param('catdir', '');
        // 文章ID、或者别名
        $id = $this->request->param('id', '', '');
        // 模型
        $model = $this->request->param('model', 0);

        $key = $this->request->param('key', '');

        if (empty($id)) {
            $this->error('参数错误');
        }

        $content = ContentModel::alias('a')->with(['category', 'models', 'content_ext'])->where('id',
            $id)->find()->toArray();
        if (empty($content)) {
            $this->error('获取数据失败');
        }
        if (empty($content['category'])) {
            $this->error('栏目信息不存在！');
        }
        if (empty($content['models'])) {
            $this->error("模型不存在！");
        }

        // 更新访问次数
        ContentModel::where('id', $id)->inc('click')->update();

        $category = $content['category'];

        View::assign('id', $id);
        View::assign('pid', $category['pid']);
        View::assign('category', $category);
        View::assign('data', $content);
        View::assign('is_pay', 0);
        View::assign('is_part_content_pay', 0);
        View::assign('readpoint', 0);

        $showTpl = "/".$content['category']['show_tpl'] ?: "article_show";

        return $this->view->fetch($showTpl);
    }

}
